package com.example.demospringboot.netty.http;

import com.example.demospringboot.netty.http.handler.HttpClientInitializer;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.DefaultFullHttpRequest;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.util.CharsetUtil;

/**
 * @author chenlei
 * @date 2020-01-07
**/
public class Client {
	
	public static void main(String[] args) {
		EventLoopGroup workerGroup = new NioEventLoopGroup();
		try {
			Bootstrap client = new Bootstrap();
			client.group(workerGroup)
			.channel(NioSocketChannel.class)
			.option(ChannelOption.SO_KEEPALIVE, true)
			.handler(new HttpClientInitializer());
			ChannelFuture f = client.connect("127.0.0.1",8090).sync();
			FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1,HttpMethod.GET,"/user/add",Unpooled.copiedBuffer("i am coming",CharsetUtil.UTF_8));
			request.headers().set(org.apache.http.HttpHeaders.CONTENT_TYPE,"text/plain;charset=utf-8");
			f.channel().writeAndFlush(request).sync();
			f.channel().closeFuture().sync();
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			workerGroup.shutdownGracefully();
		}
	}

}
